require(['jquery', '../modules/pageSize', 'Bswiper'], function($, pageSetSize, aswiper) {
	//设置html根目录字体大小
	setRootFontSize(pageSetSize);
	//图片滑动
	var aswiper = new Swiper('.swiper-container', {
		pagination: '.swiper-pagination',
		paginationClickable: true
	});
	$.ajax({
        url: '/Home/goods/getGoodsContent'+getFixedExtraUrl(),
		data: {"id" : _good_id},
        type: 'POST',
        dataType: 'json',
        success: function(rs){
            if(rs.status){
				//console.log(decodeHTMLSpecialChars(rs.data));
				$("#detail-content-ajax").html(decodeHTMLSpecialChars(rs.data));
			}else{
				$("#detail-content-ajax").html("");
			}
        },
		error: function(){
			$("#detail-content-ajax").html("");
		}
    });
});

//倒计时
require(['jquery', 'countdown'], function($) {
	if(isExistDjsTime){
		if(djs_startTimes[0] != ''){
			$('.count-down').countdown(djs_endTimes[0], function(event) {
			  if(event.type == "update"){
				$(this).html(event.strftime('距离结束还有：<span class="hour_show">%H</span>:<span class="minute_show">%M</span>:<span class="second_show">%S</span>'));
			  }else{
			  	 $(this).html(event.strftime('已经结束'));
			  }
			});
		}
	}
});

//搜索隐藏效果
require(['jquery', 'Bswiper', '../modules/slideBox'], function($, aswiper, aslide) {
	aswiper(aslide.slideVertical());
    //详情选项卡切换
    $('.goods-tab-title li').click(function(){
        $(this).addClass('active').siblings().removeClass('active');
        var likey = $('.goods-tab-title li').index(this);
        $('.goods-tab-content').hide();
        $('.goods-tab-content').eq(likey).show();
    })
})

//搜索隐藏效果
require(['jquery','Bswiper','../modules/slideBox'],function($,aswiper,aslide){
    aswiper(aslide.slideVertical());
});

//购物车添加商品效果
require(['jquery', '../modules/popup'], function($,promotion) {
	$('.bindPhone-box').addClass('bindPhone-none');
	var cartValue = 0;
	var inputValue = 1;
	var newNumber ={
		o:'',
		e:'',
		pid:''
	};
	var boolTxt = true; //确定是否弹规格弹窗
	var result;
	var _addAjax = false;
	var ajaxGoodsId = $('.ajax_goodsId');
	var goodsInfo = {
		gList:ajaxGoodsId.data('glist'),
		gListTotal:ajaxGoodsId.data('glist_total'),
		url: ajaxGoodsId.data('url'),
		cartUrl:ajaxGoodsId.data('carturl'),
		sid: ajaxGoodsId.data('sid'),
		pid: ajaxGoodsId.data("pid"),
		gid: ajaxGoodsId.data("gid"),
		spid:ajaxGoodsId.data("spid")
	};

	if(boolTxt == true) {
		$(".add-goods").click(function (e) {
			e.preventDefault();
			_global.checkPhoner.verifyfunc = function(rs){
				if(rs){
					addGoodsDemo(e);
					return true;
				}else{
					return false;
				}
			};
			_global.checkPhoner.succfunc =function(){
				addGoodsDemo(e);
			};
			_global.checkPhoner.run(e)
		});
	}

	function addGoodsDemo(e){
		//console.log(goodsInfo.gListTotal,goodsInfo.gList);
		if((goodsInfo.gList != 1 && goodsInfo.gListTotal < 2) || (goodsInfo.gList == 1 && goodsInfo.gListTotal < 2)){
			addGoodsAjax(e);
		} else {
			promotion.popupSlide('.goods-norms-popup'); // 弹出列表
			addListAjax();
		}
	}

	/**
	 * @private
	 *	addGoodsAjax
	 *	数据多条时
     */

	function addGoodsAjax(e){
		newNumber.o = $('#unique-cart-number');
		newNumber.e = $('.buy-info .txt');
		var act = 'plus';
		changeCartProduct(e, goodsInfo, 1, act,newNumber);
	}
	function addListAjax (){
		$('.operate').on('click','.plus',function(e){
			e.preventDefault();
			newNumber.o = $('#gg-cart-buy-num');
			newNumber.e = $(this).parent().find('.txt');
			newNumber.pid = $(this);
			var act = 'plus';
			changeCartProduct(e, goodsInfo, 1, act,newNumber);
		});
	}

	//减号
	$(".buy-info .num").on('click','.minus', function(e) {
		newNumber.e = $(this).next('.txt');
		newNumber.o = $('#unique-cart-number');
		var num = newNumber.e.text();
		if (num <= 0) {
			return;
		}
		//span.text(--num);
		var act = 'minus';
		changeCartProduct(e, goodsInfo, num, act,newNumber);
	});

	//减号
	$(".operate").on('click','.minus', function(e) {
		newNumber.e = $(this).next('.txt');
		var num = newNumber.e.text();
		newNumber.pid = $(this).parent().find('.plus');
		if (num <= 0) {
			return;
		}
		newNumber.o = $('#gg-cart-buy-num');
		//span.text(--num);
		var act = 'minus';
		changeCartProduct(e, goodsInfo, num, act,newNumber);
	});


	function changeCartProduct(e,goodsInfo, num, act,newNumber) {
		//console.log(newNumber);
		if(_addAjax){
			return;
		}
		var selectPid = newNumber.pid;
		var $goodsPid ='';
		if(selectPid != '') {
			$goodsPid = selectPid.data('pid');
		}
		else {
			$goodsPid = goodsInfo.pid;
		}
		_addAjax = true;
		$.ajax({
			url: goodsInfo.cartUrl,
			type: 'post',
			data: {
				'pid': $goodsPid,
				'buyNum': num,
				'goods_id': goodsInfo.gid,
				'local_id': goodsInfo.sid,
				'spid': goodsInfo.spid,
				'action': act
			},
			dataType: 'json',
			success: function(data) {
				//console.log(data);
				if(data.status == 2) {
					promotion.alertPopup(data.msg);
					result = data.status;
				}else if(data.status == 0){
					promotion.alertPopup(data.msg);
					window.location.href=data.url;
				}else{
					e.preventDefault();
					//cartValue = $('.fixed-bot-cart span').text();
					//inputValue = $('#number').val();
					if (act == '' || act != 'minus') {
						runBallEffect(e);
						//newNumber = parseInt(cartValue) + parseInt(inputValue);
						newNumber.o.text(data.quanbu);
						newNumber.e.text(data.product);
						$('#only-price').text('￥'+data.money);
						$('.add_number').addClass('animate').html('+' + inputValue);
						setTimeout(function() {
							$('.add_number').removeClass('animate');
						}, 500)
					} else {
						//newNumber = parseInt(cartValue) - 1;
						newNumber.o.text(data.quanbu);
						$('#only-price').text('￥'+data.money);
						newNumber.e.text(data.product);
					}
				}
				_addAjax = false;
			},
			error: function(err) {
				promotion.alertPopup('服务器异常，请稍后重试！')
				console.log(err.toString());
				_addAjax = false;
			}

		});
	}

	$('.spec_info_close').on('click',function () {
		 $('#unique-cart-number').text($('#gg-cart-buy-num').text());
		// var changeNumber = $(".buy-info .txt").text();
		// console.log(changeNumber);
	})


});

// 收藏效果
require(['jquery'], function($) {
	var _gd_collect = $(".goods-collect");
	var $type ='';
	_gd_collect.click(function(e){
		_global.checkPhoner.verifyfunc = function(rs){
			if(rs){
				if(_gd_collect.hasClass("active")){
					$type = 'n';
				}else{
					$type = 'y';
				}
				productLike($type);
				return true;
			}
			else{
				return false;
			}
		};
		_global.checkPhoner.succfunc = function(){
			if(_gd_collect.hasClass("active")){
				$type = 'n';
			}else{
				$type = 'y';
			}
			productLike($type);
		};
		_global.checkPhoner.run(e, $(this).attr('href'));
	});
    function productLike($type){
		//console.log($type);
        $.post("/Home/Member/productLike" + getFixedExtraUrl(), {"product_id": _gd_collect.data('product-id'),"local_id": _gd_collect.data('local-id'),"type":$type},
            function(data){
                if(data.stat){
                    _gd_collect.toggleClass('active');
                    _global.promote.alertPopup(data.msg);
                    //_gd_collect.off('click');
                }else{
                    _global.promote.alertPopup(data.msg);
                }
            }, "json");
    }

});

//弹窗
require(['jquery', '../modules/popup'], function($, promotion) {
	//促销信息弹窗
	$('.goods-promotion-info').click(function() {
		promotion.popupSlide('.goods-promotion-popup');
	});

	//客服帮助弹窗
	$('.service-tel').click(function() {
		promotion.popup('.alert-popup');
	})
});

// 优惠兑换弹窗 
require(['jquery'],function($){

	$('.coupon-btn').click(function(e) {
        _global.checkPhoner.verifyfunc = function(rs){
			if(rs){
				_global.promote.popup('.coupon-redeem-box');
				couponMtd();
				return true;
			}
			else{
				return false;
			}
		};
		_global.checkPhoner.succfunc = function(){
			_global.promote.popup('.coupon-redeem-box');
			$('.red-inputInfo').show();
		    $('.cup-success').hide();
		    $('.red-code-tips').show();
		    $('.red-code-tips').text('请输入你的兑换码!');
		    $('#cdKey').val('');
			couponMtd();
		};
		_global.checkPhoner.run(e);
    });

    function couponMtd(){
    	var _isAjaxCup = false;
    	var tips = $('.red-code-tips');
    	//改变验证框效果
        $('#cdKey').keyup(function() {
            $(this).parent().find('a').css({'background': '#3c6a63','color': '#fff'});
        });
    	$('#cup_btn').on('click',function(e){
    		e.preventDefault();
    		var _cdKey = $('#cdKey').val();
    		if(_isAjaxCup) return;
    		if(_cdKey ==''){
    			tips.text('请输入你的兑换码!');
        	}else{
        		_isAjaxCup = true;
	            $.ajax({
	                url:'/Home/Member/ecCode'+getFixedExtraUrl(),
	                type:'post',
	                dataType:'json',
	                data:{'code':_cdKey},
	                success:function(rs){
						//console.log(rs)
						if(rs.status == 1){
							$('.red-inputInfo').hide();
							tips.hide();
							$('.cup-success span').text(rs.msg);
							$('.cup-success').show();
							var timeout;
		                    var count = 3; // 默认时间
		                    var BtnCount = function() {
		                        // 启动按钮
		                        if (count == 0) {
		                            clearTimeout(timeout);   // 可取消由 setTimeout() 方法设置的 timeout
		                            $('.coupon-redeem-box').hide();	 	                                                     
		                            $('.pop-mask').css({display:'none'});
		                             _isAjaxCup = false;
		                        }
		                        else {
		                            count--;
		                            $('.cup-success').find('i').text(count.toString());
		                            setTimeout(BtnCount, 1000);
		                        }
		                    };
		                    timeout = setTimeout(BtnCount, 1000); // 1s执行一次BtnCount							  
						}else{
						    tips.text(rs.msg);
						    _isAjaxCup = false;
						}
	                },
	                error:function(){
	                	 _isAjaxCup = false;
	                    _global.promote.alertPopup('服务器异常，请稍后重试！');
	                }
	            })
       		}
    	})
    }
})